var express = require('express');
let multer = require('multer');
let fs = require("fs");
let path = require("path");
var router = express.Router();
var model = require('../model/tour')
const auth  = require('../middleware/auth')
const config = require('../config')
/* GET home page. */
router.get('/lists', function (req, res, next) {
    model.doQuery(req.query, function succ(results) {
        // results.forEach(el => {
        //     console.log('el.img',el.img, config.imgPath(el.img));
        //     el.img = config.imgPath(el.img)
        // })
        
        if (req.query.callback) {
            // res.type('text/html;charset=utf-8')
            return res.jsonp({
                status: 200,
                data: results
            })
        }
        res.send({
            status: 200,
            data: results
        })
    }, function fail(err) {
        res.send({
            status: 200,
            data: err
        })
    })

});

router.post('/add', auth,function (req, res, next) {
    // 添加用户信息，插入数据库
    req.body.username = req.profile.username;
    model.doInsert(req.body, function succ(results) {
        res.send({
            status: 200,
            data: results
        })
    }, function fail(err) {
        res.send({
            status: 200,
            data: err
        })
    })

});
router.post('/update', auth,function (req, res, next) {
     // 添加用户信息，插入数据库
     req.body.username = req.profile.username;
    model.doUpdate(req.body, function succ(results) {
        res.send({
            status: 200,
            data: results
        })
    }, function fail(err) {
        res.send({
            status: 200,
            data: err
        })
    })

});
router.delete('/delete', auth,function (req, res, next) {
    model.doDelete(req.body.id, function succ(results) {
        res.send({
            status: 200,
            data: results
        })
    }, function fail(err) {
        res.send({
            status: 200,
            data: err
        }) 
    })

});
let uploadPath = path.resolve(__dirname,'../public/upload/');
uploadPath = path.join(uploadPath, '/')
console.log('uploadPath', uploadPath, __dirname);
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, uploadPath)
    },
    // filename: function (req, file, cb) {
    //   cb(null, file.fieldname + '-' + Date.now())
    // }
  })
  
const upload = multer({
    //设置文件存储路径
    dest: uploadPath,  //upload文件如果不存在则会自己创建一个
    // storage
});
router.post('/upload', auth, upload.single('file'), function (req, res) {
    let file = req.file;
    let fileInfo = {};
    const filename = file.filename + path.extname(file.originalname)
    fs.renameSync(uploadPath + file.filename, uploadPath + filename)
    fileInfo.mimetype = file.mimetype;
    fileInfo.originalname = file.originalname;
    fileInfo.size = file.size;
    fileInfo.filename = filename
    fileInfo.path = config.imgPath(filename)

    res.send({
        status: 200,
        data: fileInfo
    })
});


module.exports = router;


